<?php
namespace app\common\service;

use think\Db;
use http\Http;
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2025/3/10
 * Time: 17:29
 */
class GuguService
{
	private static $address = "https://api.gugudata.com"; //接口地址

    public static function make($action,$dataJson)
	{
		$data = [];
		try {
			if(empty($action)){
				//return [0=>'404',1=>'action参数不能为空'];
				throw new \Exception("action参数不能为空！",404);
			}
			if(!in_array($action,['/location/college','/metadata/ceemajor','/metadata/ceecollegeline','/metadata/ceemajorline','/metadata/college-enrollment-plan','/metadata/ceeline/one-score-one-section','/metadata/college-score-detail'])){
				//return [0=>'404',1=>'action参数不正确'];
				throw new \Exception("action参数不正确！",404);
			}
			$url = self::$address.$action;
			$return_json = Http::get($url,$dataJson);
			//$query_string = is_array($dataJson) ? http_build_query($dataJson) : $dataJson;
			//$geturl = $query_string ? $url . (stripos($url, "?") !== false ? "&" : "?") . $query_string : $url;
			//$return_json = file_get_contents($geturl);
			$return_data = json_decode($return_json,true);
			if(empty($return_data['DataStatus'])){
				//return [0=>'-1',1=>'请求失败'];
				throw new \Exception($return_data['msg']??"请求失败1！",-1);
			}
			if($return_data['DataStatus']['StatusCode'] != 100){
				//return [0=>'-1',1=>$return_data['DataStatus']['StatusDescription']];
				throw new \Exception($return_data['DataStatus']['StatusDescription'],-1);
			}
			$data = [$return_data['DataStatus'],$return_data['Data']];
		} catch (\Exception $e) {
            //出现异常记录日志
			setLog('gugu','接口：'.$action.'，文件：'.$e->getFile().',行数：'.$e->getLine().',错误信息：'.$e->getMessage().'。请求参数：'.json_encode($dataJson,JSON_UNESCAPED_UNICODE).'。返回数据：'.($return_json??''));
			return [0=>'500',1=>$e->getMessage()];
        }
		
		return [0=>'200',1=>$data];
	}

}